package com.hualan.sfzy.controller;

import com.hualan.sfzy.entity.Cspi;
import com.hualan.sfzy.entity.Fm;
import com.hualan.sfzy.service.FmService;
import com.hualan.sfzy.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/fm")
public class FmController {

    @Autowired
    private FmService fmService;

    @GetMapping("/all")
    public Result findAll(){
        List<Fm> list = fmService.findFmAndFmClassificationAll();
        if(list.isEmpty()){
            return Result.error("查找不到商品");
        }
        return Result.success(list);
    }

    @GetMapping
    public Result fineById(Integer id) {
        Fm fm = fmService.findById(id);
        if (fm == null) {
            return Result.error("查询成功");
        }
        return Result.success(fm);
    }

    @GetMapping("/findFmAndFmClassificationById")
    public Result findFmAndFmClassificationById(Integer id)  {
        Fm fm = fmService.findFmAndFmClassificationById(id);
        if (fm != null) {
            return Result.success(fm);
        }
        return Result.error("查询失败");
    }

    @GetMapping("/page")
    public Result findPage(Fm fm, Integer pageNum, Integer pageSize){
        Map<String,Object> map = fmService.findPage(pageNum,pageSize,fm);
        return Result.success(map);
    }

    @DeleteMapping()
    public Result remove(@RequestParam Integer id){
        int count = fmService.remove(id);
        if(count > 0){
            return Result.success("删除成功");
        }
        return Result.error("删除失败");
    }

    @DeleteMapping("/batch")
    public Result removeBatch(@RequestParam Integer[] ids){
        int count = fmService.removeBatch(ids);
        if(count > 0){
            return Result.success("删除成功");
        }
        return Result.error("删除失败");
    }

    @PostMapping("/add-selective")
    public Result addSelective(@RequestBody Fm fm){
        int count = fmService.addSelective(fm);
        if(count > 0){
            return Result.success("添加成功");
        }
        return Result.error("添加失败");
    }

    @PutMapping
    public Result editSelective(@RequestBody Fm fm){
        int count = fmService.edit(fm);
        if(count > 0){
            return Result.success("修改成功");
        }
        return Result.error("修改失败");
    }

}
